package com.why.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.dao.ProductCategoryMapper;
import com.why.dao.ProductMapper;
import com.why.pojo.Product;
import com.why.pojo.ProductCategory;
import com.why.service.ProductCategoryService;
import com.why.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    ProductCategoryMapper productCategoryMapper;

    @Autowired
    ProductService productService;

    @Override
    public List<ProductCategory> proCategoryList(int parentId) {
        return productCategoryMapper.proCategoryList(parentId);
    }


    public List<List<ProductCategory>> proCategoryListByType() {
        List<ProductCategory> categoryList1 = productCategoryMapper.proCategoryListByType(1);
        List<ProductCategory> categoryList2 = productCategoryMapper.proCategoryListByType(2);
        List<ProductCategory> categoryList3 = productCategoryMapper.proCategoryListByType(3);
        List<Product> products = productService.selectProductList();

        List mapList = new ArrayList();
        mapList.add(categoryList1);
        mapList.add(categoryList2);
        mapList.add(categoryList3);
        mapList.add(products);
        return mapList;
    }

    @Override
    public PageInfo<ProductCategory> allCategoryList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductCategory> categoryList = productCategoryMapper.allCategoryList(pageNum, pageSize);
        PageInfo<ProductCategory> pageInfo = new PageInfo<>(categoryList);
        return pageInfo;
    }

    @Override
    public boolean insertSelective(ProductCategory record) {
        if (productCategoryMapper.insertSelective(record)==1){
            return true;
        }
        return false;
    }

    @Override
    public ProductCategory selectByPrimaryKey(Integer id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateByPrimaryKey(ProductCategory record) {
        if (productCategoryMapper.updateByPrimaryKeySelective(record)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteByPrimaryKey(Integer id) {
        if (productCategoryMapper.deleteByPrimaryKey(id)==1){
            return true;
        }
        return false;
    }

    @Override
    public int selectCount(Integer parentId) {
        return productCategoryMapper.selectCount(parentId);
    }

    @Override
    public List<ProductCategory> categoryByType(int type) {
        return  productCategoryMapper.proCategoryListByType(type);
    }

    @Override
    public boolean categoryExist(ProductCategory productCategory) {
        if (productCategoryMapper.categoryExist(productCategory)==1){
            return true;
        }
        return false;
    }
}
